#include "widget.h"
#include "ui_widget.h"
#include <QHostAddress>
#include <QCryptographicHash>
#include <QMessageBox>
#include <QTime>

QString ifmd5;
QString elsemd5;

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);

    setWindowTitle("登录");

    //客户端建立
    tcpSocket = new QTcpSocket;
    tcpSocket->connectToHost("www.huaniaopet.com",5556,QTcpSocket::ReadWrite);
    if(tcpSocket->waitForConnected()) {
//        qDebug()<<"连接成功";
        ui->label_3->setText("服务器状态：连接成功");
    }
    connect(tcpSocket, &QTcpSocket::readyRead,
            [=]()
            {
                //从通信套接字中取出内容
                QByteArray array = tcpSocket->readAll();
                QString res = QString(array);
                //传入全局
                ifmd5 = res;


                //开始第二次验证
                QString phpname = ui->name->text();
                QString phppwd = ui->pwd->text();

                QNetworkAccessManager *manager = new QNetworkAccessManager(this);
                //URL
                QString baseUrl = "http://www.huaniaopet.com/qt.php/";
                QUrl url(baseUrl);

                // 表单数据
                QByteArray dataArray;
                dataArray.append("name=");
                dataArray.append(phpname);
                dataArray.append("&");
                dataArray.append("pwd=");
                dataArray.append(phppwd);

                // 构造请求
                QNetworkRequest request;
                request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
                request.setUrl(url);

                QNetworkReply *pReplay = manager->post(request, dataArray);


                QEventLoop eventLoop;
                QObject::connect(manager, &QNetworkAccessManager::finished, &eventLoop, &QEventLoop::quit);
                eventLoop.exec();

                QString res1 = pReplay->readAll();
                //传入全局
                elsemd5 =res1;


                qDebug() << res1;
                qDebug() << res;
                //获取二次验证码结束


                if(res != res1){
                    int max = 20;
                    QString tmp = QString("0123456789ABCDEFGHIJKLMNOPQRSTUVWZYZ");
                    QString str = QString();
                    QTime t;
                    t= QTime::currentTime();
                    qsrand(t.msec()+t.second()*1000);
                    for(int i=0;i<max;i++) {
                        int ir = qrand()%tmp.length();
                        str[i] = tmp.at(ir);
                    }
                    QMessageBox::critical(0,"提示",str);



                }else{
                    new_win = new new_window;
                    new_win->show();

                }
            }

            );
}

Widget::~Widget()
{
    delete ui;
}

void Widget::on_pwd_textChanged(const QString &arg1)
{
    ui->pwd->setEchoMode(QLineEdit::Password);
}

void Widget::on_login_clicked()
{


    //获取编辑框内容
    QString name = ui->name->text();
    QString pwd = ui->pwd->text();


    //加密字符串
    QString md51,md52;
    QByteArray ba,bb,bc,bd;
    QCryptographicHash md(QCryptographicHash::Md5);
    QCryptographicHash md2(QCryptographicHash::Md5);

    ba.append(name);
    md.addData(ba);
    bb = md.result();
    QString namemd5 = md51.append(bb.toHex());
    QString name2 = namemd5.mid(0,16);


    bc.append(pwd);
    md2.addData(bc);
    bd = md2.result();
    QString pwdmd5 = md52.append(bd.toHex());
    QString pwd2 = pwdmd5.mid(16);

    QString send = name2+pwd2;

//    qDebug()<< send;
    //发送数据
    tcpSocket->write( send.toUtf8().data());
}
